package com.nickstumpos.android.easyVolume.preset.presentation;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.nickstumpos.android.easyVolume.R;
import com.nickstumpos.android.easyVolume.constants.EVCONSTANTS;
import com.nickstumpos.android.easyVolume.preset.types.PresetIdentifier;

public class TextEntryActivity extends Activity {

	private SharedPreferences					settings;
	private SharedPreferences.Editor	editor;
	private EditText									et;
	private PresetIdentifier					button;
	private Button										save;
	private Button										cancel;

	public TextEntryActivity() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void finish() {
		super.onDestroy();
		super.finish();
	}

	private String getButtonTitle() {
		String title = null;
		if (this.button != null) {
			switch (this.button) {
				case ONE:
					title = this.settings.getString(EVCONSTANTS.ONE_NAME, this.button
							.getName());
					break;
				case TWO:
					title = this.settings.getString(EVCONSTANTS.TWO_NAME, this.button
							.getName());
					break;
				case THREE:
					title = this.settings.getString(EVCONSTANTS.THREE_NAME, this.button
							.getName());
					break;
				case FOUR:
					title = this.settings.getString(EVCONSTANTS.FOUR_NAME, this.button
							.getName());
					break;
				case FIVE:
					title = this.settings.getString(EVCONSTANTS.FIVE_NAME, this.button
							.getName());
					break;
				case SIX:
					title = this.settings.getString(EVCONSTANTS.SIX_NAME, this.button
							.getName());
					break;
				default:
					title = this.button.getName();
			}
		}
		return title;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.settings = this.getSharedPreferences(EVCONSTANTS.PREFS_NAME, 0);
		this.editor = this.settings.edit();
		this.setContentView(R.layout.edittextlayout);
		this.et = (EditText) this.findViewById(R.id.EditText01);
		this.save = (Button) this.findViewById(R.id.SaveButton);
		this.cancel = (Button) this.findViewById(R.id.CancelText);
		this.save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				TextEntryActivity.this.saveText();

			}

		});
		this.cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				TextEntryActivity.this.finish();

			}

		});
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		Bundle bundle = this.getIntent().getExtras();
		this.button = (PresetIdentifier) bundle
				.getSerializable(EVCONSTANTS.CURRENT_EDIT_FIELD);
		String title = this.getButtonTitle();
		if (title == null) {
			this.finish();
		}
		this.et.setText(title);
	}

	private void saveText() {
		switch (this.button) {
			case ONE:
				this.editor.putString(EVCONSTANTS.ONE_NAME, this.et.getText()
						.toString());
				break;
			case TWO:
				this.editor.putString(EVCONSTANTS.TWO_NAME, this.et.getText()
						.toString());
				break;
			case THREE:
				this.editor.putString(EVCONSTANTS.THREE_NAME, this.et.getText()
						.toString());
				break;
			case FOUR:
				this.editor.putString(EVCONSTANTS.FOUR_NAME, this.et.getText()
						.toString());
				break;
			case FIVE:
				this.editor.putString(EVCONSTANTS.FIVE_NAME, this.et.getText()
						.toString());
				break;
			case SIX:
				this.editor.putString(EVCONSTANTS.SIX_NAME, this.et.getText()
						.toString());
				break;
		}
		this.editor.commit();
		this.finish();
	}
}
